//
//  Kirogi.swift
//  apple
//  Kirogi 鸿雁 朝鲜 一种候鸟
//  Created by admin on 2021/7/9.
//

/**
 均线选股 当ma1>ma2时标记为买入
 */
import Foundation
import Alamofire

class Kirogi{

    var date:String = ""
    var signal = 0.92
    var type:String = "etf"
    
    private var _speed:Int = 5
    var speed:Int{
        set{
            if _speed == newValue{
                return
            }else{
                _speed = newValue
            }
        }
        get{
            return _speed
        }
    }
    lazy var pushvc: UIViewController = {
        let vc = UIViewController()
        vc.view.backgroundColor = UIColor.yellow
        return vc
    }()
    lazy var param: [String : [SettingParam]] = {
        var param: [String : [SettingParam]] =  ["几百":
                        [
                            SettingParam(name: "测试", key: "speed1", value: 5),
                            SettingParam(name: "测试2", key: "speed2", value: 5.0)
                        ],
                    "几百1":
                        [
                            SettingParam(name: "测试", key: "speed3", value: Date()),
                            SettingParam(name: "测试2", key: "speed4", value: self.pushvc)
                        ],
            ]
        return param
    }()

    
   
    
    

    init() {
       
//        self.name = "Kirogi"
//        if UserDefaults.standard.float(forKey: "kirogi.signal")>0 {
//            self.signal = UserDefaults.standard.float(forKey: "kirogi.signal")
//
//        }
//        if UserDefaults.standard.float(forKey: "kirogi.speed")>0 {
//            self.speed = UserDefaults.standard.integer(forKey: "kirogi.speed")
//        }
    }
    
//    var param:String{
//        return "天数\(speed)/信号量\(signal)"
//    }
    // 数据存储表
    var tableName:String?{
        
        return "kirogi_etf_signal_\(speed)"
    }
    
     func setting()  {
//        let setvc = KirogiSettingVC()
//        setvc.kirogi = self
//        KWindow??.rootViewController?.present(setvc, animated: true, completion: nil)
    }
     func recommend(didchange:@escaping ([[String:Any]])->()) {
        
        
        let url = "\(baseurl)/select"
        let param = ["sql":"""
            select t1.code,t1.sort,t1.count,t1.signal, t2.name
            from kirogi_\(type)_signal_\(speed) as t1
            LEFT JOIN \(type)basic as t2
            ON t1.code = t2.code
            LEFT JOIN \(type)daily as t3 ON t1.code=t3.code and t3.date='\(date)'
            where t1.date='\(date)' and t1.signal>=\(signal) and t3.vol>100000000
            ORDER BY t1.signal  DESC
            """]
        
        AF.request(url, method: .post, parameters: param,encoder:JSONParameterEncoder.default)
            .responseModel([[String:Any]].self) { result in
                switch result{
                case .success(let value):
                  
                    didchange(value)
                case .failure(_):
                    break
                }
            }
    }

 

 
    
}

extension Kirogi:SchemeProtocol{
 
   
    
    
    var name: String {
       return "Kirogi"
    }
    func signalDatas(_ code:String) throws -> [[String:Any]]{
        return []
    }
    
   
    
    
}

